from torch import nn
class NestMLP(nn.Module):
    def __init__(self):
        super().__init__()
        self.net = nn.Sequential(nn.Linear(20, 64), nn.ReLU(),
                                 nn.Linear(64, 32), nn.ReLU())
        self.linear = nn.Linear(32, 16)

    def forward(self, X):
        return self.linear(self.net(X))

model = NestMLP()

# 访问net层的参数
print('访问net层的参数')
for name, param in model.net.named_parameters():
    print(f"Parameter name: {name}, shape:{param.shape}, Parameter: {param}")

# 访问linear层的参数
print('访问linear层的参数')
for name, param in model.linear.named_parameters():
    print(f"Parameter name: {name}, shape:{param.shape}, Parameter: {param}")